<?php

namespace app\admin\controller;
use app\base\controller\Base;
use think\Request;
use think\Cache;
use think\Config;
use think\Session;

//推广
class Generalize extends Base
{
    public  $model;

    public function __construct()
    {
   $this->model = model("Generalize");
        parent::__construct();
    }

    /**
     * 显示推广链接 代理商推广管理
     *
     * @return \think\Response
     */
    public function index()
    {//
        $where = [];

       if($this->type==1){
           $where["a.type"] =1;

           if($this->agents_ids!="all"){
               $where['admin_agent.id'] = $this->sidRows['agent_id'];
           }
           $res = $this->model->Generalize_page_daili($where);
       }else{
           $where["a.type"] =2;
           if($this->agents_ids!="all"){
               $where['admin_staff.agent_id'] = $this->sidRows['agent_id'];
           }

           $res = $this->model->Generalize_page_yg($where);
       }

        //时间的处理

     return   resJson(1,$this->ecLang("query"),$res['res'],$res['pageInfo']);



    }


    public function createcode(){
        $baseRedeemCode = "";
        $GeneralizeResult = $this->model->where("id",$this->id)->find();

        if(!empty($GeneralizeResult['redeem_code'])){
            return   failJson("暂无数据");
        }
        $Generalizecount = $this->model->count();
        $redeem_code = bcadd($Generalizecount,1);
        $redeem_code = getZero5($redeem_code);
      $GeneralizeData = [
            "redeem_code"=>$baseRedeemCode.$redeem_code,
        ];
       $res =  $this->model->save($GeneralizeData,['id'=>$this->id]);
       if(empty($res)){
           return   failJson("暂无数据");
       }
        return   sucessJson("查询成功",$res);
    }


    public function read($id){
        //搜索当前显示
       $res =  $this->model->where(['id'=>$id])->find();
       if(!empty($res)){
           if($res['type']==1){
               // 代理商
                $dailiModel = new \app\admin\model\AdminAgent;
             $daili =   $dailiModel->where(['id'=>$res['association_id']])->find();
             $res['agent_name']  = $daili['agent_name'];
             $res['contacts'] = $daili['contacts'];


           }else{
               // 代理商
               $dailiModel = new \app\admin\model\AdminStaff;
               $daili =   $dailiModel->where(['id'=>$res['association_id']])->find();
               $res['staff_name']  = $daili['staff_name'];

           }
           return sucessJson("查询成功",$res);
       }
        return   failJson("暂无数据");

    }





}
